//
//  PortfilioSubViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/3.
//  Copyright © 2020 王洋. All rights reserved.
//


#import "CommonOpusModel.h"
#import "CommonOpusCatModel.h"
#import "XLPageViewController.h"
#import "PortfilioSubViewController.h"
#import "PortfilioSubTitleCell.h"

@interface PortfilioSubViewController ()
<XLPageViewControllerDelegate,
XLPageViewControllerDataSrouce>

@property (nonatomic, strong) XLPageViewController *pageViewController;

@property (nonatomic, strong) NSMutableArray <CommonOpusCatModel *>*catArray;


@end

@implementation PortfilioSubViewController

- (instancetype)initWithType:(PortfilioSubType)type AndStoreId:(NSString *)storeId classId:(NSString *)classId {
    self = [super init];
    
    if (self) {
        self.type = type;
        self.storeId = storeId;
        self.classId = classId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self request_PortCat];
}

- (void)initPageViewController {
    
    XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
    config.shadowLineColor = rgba(254, 118, 15, 0);
    config.titleSelectedFont = [UIFont boldSystemFontOfSize:14];
    config.titleNormalFont = [UIFont systemFontOfSize:14];
    config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
    config.separatorLineHidden = YES;
    config.shadowLineAnimationType = XLPageShadowLineAnimationTypeZoom;
    config.shadowLineWidth = 0;
    config.titleSpace = 10;
    config.titleWidth = 50;
    config.titleViewHeight = 44;
    
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:config];
    self.pageViewController.view.frame = self.view.bounds;
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController registerClass:PortfilioSubTitleCell.class forTitleViewCellWithReuseIdentifier:@"PortfilioSubTitleCellID"];
}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {
    
    PortfilioListViewController *vc = [[PortfilioListViewController alloc]
                                       initWithType:self.type
                                       storeId:self.storeId classId:self.classId AndSocId:self.catArray[index].socId];
    return vc;
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return self.catArray[index].opusClassName;
}

- (NSInteger)pageViewControllerNumberOfPage {
    return self.catArray.count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
    NSLog(@"切换到了：%@",self.catArray[index]);
}

- (__kindof XLPageTitleCell *)pageViewController:(XLPageViewController *)pageViewController titleViewCellForItemAtIndex:(NSInteger)index {
    PortfilioSubTitleCell *cell = [pageViewController dequeueReusableTitleViewCellWithIdentifier:@"PortfilioSubTitleCellID" forIndex:index];
    cell.titleLabel.text = self.catArray[index].opusClassName;
    cell.textLabel.text = @"";
    return cell;
}

- (void)request_PortCat {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    NSMutableDictionary *t = @{}.mutableCopy;

    [dic setValue:@(1) forKey:@"pageNum"];
    [dic setValue:@(20) forKey:@"pageSize"];

    [t setValue:self.storeId forKey:@"sellerId"];
    
    if ([self.classId isEqualToString:@"7"]) {
        switch (self.type) {
            case PortfilioSubType_kezhao:{
                [t setValue:@(2) forKey:@"opusType"];
            } break;
            case PortfilioSubType_yangzhao:{
                [t setValue:@(1) forKey:@"opusType"];
            } break;
                
            default:
                break;
        }
    }

    [dic setValue:t forKey:@"t"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_StoreCase];
    
    [SVProgressHUD showWithStatus:@"正在加载分类"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *sellerOpusClassificationList = [resultDic valueForKey:@"sellerOpusClassificationList"];
                weakSelf.catArray = @[].mutableCopy;
                if ([sellerOpusClassificationList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sellerOpusClassificationList.count; i++) {
                        NSDictionary *sellerOpusClassificationListDic = sellerOpusClassificationList[i];
                        CommonOpusCatModel *model = [CommonOpusCatModel modelWithDictionary:sellerOpusClassificationListDic];
                        [weakSelf.catArray addObject:model];
                    }
                }
                if (weakSelf.catArray.count > 0) {
                    [weakSelf initPageViewController];
                } else {

                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
    
}

@end
